package com.jinzhi.jzweb.controller;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.jinzhi.common.utils.ShiroUtils;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.domain.JazUserIntegralDO;
import cn.hutool.core.date.DateTime;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazSysBannerDO;
import com.jinzhi.jzweb.service.JazSysBannerService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * banner管理
 * </pre>
 * <small> 2023-04-21 20:39:18 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazSysBanner")
public class JazSysBannerController extends BaseController {
	@Autowired
	private JazSysBannerService jazSysBannerService;

	@GetMapping()
	@RequiresPermissions("jzweb:jazSysBanner:jazSysBanner")
	String JazSysBanner(){
	    return "jzweb/jazSysBanner/jazSysBanner";
	}

	@Log(value = "banner上下移",sysModule = 11,operationType = 3)
	@ResponseBody
	@GetMapping("/bannerMove")
	public Result<Page<JazSysBannerDO>> bannerMove(@RequestParam("id") Long id,@RequestParam("move") String move){
		// 查询列表数据
		Page<JazSysBannerDO> page = jazSysBannerService.selectPage(getPage(JazSysBannerDO.class),
				jazSysBannerService.convertToEntityWrapper().orderBy("sort",true).orderBy("id", false));

		JazSysBannerDO jazSysBannerDOs = jazSysBannerService.selectById(id);
		if(move.equals("1")){
			//第一条无法上移
			if(jazSysBannerDOs.getSort().equals(1)){
				return Result.fail("第一条数据无法上移");
			}
		}else if(move.equals("2")){
			//最后一条无法下移
			if(jazSysBannerDOs.getSort().equals(page.getRecords().size())){
				return Result.fail("最后一条数据无法下移");
			}
		}
		for (int i = 0; i < page.getRecords().size(); i++) {
			if(move.equals("1")){	//上移
				if(page.getRecords().get(i).getId().equals(id)){
					//更新当前banner的序号
					page.getRecords().get(i).setSort(page.getRecords().get(i-1).getSort());
					JazSysBannerDO jazSysBannerDO = jazSysBannerService.selectById(page.getRecords().get(i).getId());
					jazSysBannerDO.setSort(page.getRecords().get(i-1).getSort());
					jazSysBannerService.updateById(jazSysBannerDO);

					//更新上一个banner的序号
					page.getRecords().get(i-1).setSort(page.getRecords().get(i).getSort()+1);
					JazSysBannerDO jazSysBannerDO1 = jazSysBannerService.selectById(page.getRecords().get(i-1).getId());
					jazSysBannerDO1.setSort(page.getRecords().get(i).getSort()+1);
					jazSysBannerService.updateById(jazSysBannerDO1);
				}
			}else if(move.equals("2")){	//下移
				if(page.getRecords().get(i).getId().equals(id)){
					//更新当前banner的序号
					page.getRecords().get(i).setSort(page.getRecords().get(i+1).getSort());
					JazSysBannerDO jazSysBannerDO = jazSysBannerService.selectById(page.getRecords().get(i).getId());
					jazSysBannerDO.setSort(page.getRecords().get(i+1).getSort());
					jazSysBannerService.updateById(jazSysBannerDO);

					//更新下一个banner的序号
					page.getRecords().get(i+1).setSort(page.getRecords().get(i).getSort()-1);
					JazSysBannerDO jazSysBannerDO1 = jazSysBannerService.selectById(page.getRecords().get(i+1).getId());
					jazSysBannerDO1.setSort(page.getRecords().get(i).getSort()-1);
					jazSysBannerService.updateById(jazSysBannerDO1);
				}
			}

		}
		return Result.ok(page);
	}


	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:jazSysBanner:jazSysBanner")
	public Result<Page<JazSysBannerDO>> list(JazSysBannerDO jazSysBannerDTO,String current,String size){
		// 查询列表数据
		Page<JazSysBannerDO> page1 = new Page<JazSysBannerDO>(Integer.valueOf(current),Integer.valueOf(size));
		Page<JazSysBannerDO> page = jazSysBannerService.selectPage(page1,
					jazSysBannerService.convertToEntityWrapper().orderBy("sort",true).orderBy("id", false));

		return Result.ok(page);
	}


	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazSysBanner:add")
	String add(){
	    return "jzweb/jazSysBanner/add";
	}

	@Log(value = "新增banner",sysModule = 11,operationType = 1)
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazSysBanner:add")
	public Result<String> save( JazSysBannerDO jazSysBanner){
		//统计banner条数
		EntityWrapper<JazSysBannerDO> wrapper = new EntityWrapper<JazSysBannerDO>();
		int count = jazSysBannerService.selectCount(wrapper);
		//设置新banner序号、创建时间、创建人id、
		jazSysBanner.setSort(count+1);
		jazSysBanner.setCreateTime(new DateTime());
		jazSysBanner.setUserId(getUserId());
		//状态默认启动
		jazSysBanner.setStatus(0);
		jazSysBannerService.insert(jazSysBanner);
		return Result.ok();
	}


	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazSysBanner:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazSysBannerDO jazSysBanner = jazSysBannerService.selectById(id);
		model.addAttribute("jazSysBanner", jazSysBanner);
	    return "jzweb/jazSysBanner/edit";
	}
	
	@Log(value = "编辑并更新banner",sysModule = 11,operationType = 3)
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:jazSysBanner:edit")
	public Result<String>  update( JazSysBannerDO jazSysBanner){
		//设置banner更新时间
		jazSysBanner.setUpdateTime(new DateTime());
		boolean update = jazSysBannerService.updateById(jazSysBanner);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log(value = "删除单个banner",sysModule = 11,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazSysBanner:remove")
	public Result<String>  remove( Long id){
		jazSysBannerService.deleteById(id);
        return Result.ok();
	}

}
